/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Threads;

import Model.Customer;
import Model.ExternalDBManager;
import Model.InternalDBManager;
import Model.ProductPurchase;
import Model.Purchase;
import Supermarket.PurchaseXMLManager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

/**
 *
 * @author Manolis + Bagi
 */
public class Threads {
    
 private int availabaleTransactions = 2;
 private final int NO_OF_THREADS = 10;
 private SalesThread[] salesThread = new SalesThread[NO_OF_THREADS];
 private EntityManager em, exm;
 private Customer customer = new Customer();
 private List<Customer> AllCustomersList = new ArrayList<>();

 
 public Threads(){
     
     this.exm = ExternalDBManager.getEm();
     this.em = InternalDBManager.getEm();
     if(!em.getTransaction().isActive())
         em.getTransaction().begin();
     for (int counter = 0; counter < NO_OF_THREADS; counter++){
        salesThread[counter] = new SalesThread(em, exm, 
                        "Thread" + String.valueOf(counter), this);
        salesThread[counter].start();
  
     }
     for (int counter = 0; counter < NO_OF_THREADS; counter++){
         try{
             salesThread[counter].join();
         }catch(InterruptedException e){
             System.exit(0);
         }
     }
     File xmlFile = new File("..\\purchases.xml");
     PurchaseXMLManager purchaseXMLWriter = new PurchaseXMLManager(xmlFile);
     exportPurchases(purchaseXMLWriter);
     
 }
 
     private void exportPurchases(PurchaseXMLManager purchaseXMLWriter){
        if(!em.getTransaction().isActive())
            em.getTransaction().begin();
        TypedQuery<Purchase> allPurchasesQuery = em.createNamedQuery("Purchase.findAll", Purchase.class);
        List<Purchase> purchaseResults = allPurchasesQuery.getResultList();                    
        purchaseXMLWriter.writeXML(purchaseResults);
        TypedQuery<ProductPurchase> allPPurchasesQuery = em.createNamedQuery("ProductPurchase.findAll", ProductPurchase.class);
        List<ProductPurchase> ppurchaseResults = allPPurchasesQuery.getResultList();                    
        
        
        
        System.out.println ( "Το αρχείο XML δημιουργήθηκε με επιτυχία στο ..\\purchaise.xml" );                    
        try {
            if(!em.getTransaction().isActive())
                em.getTransaction().begin();
            for (ProductPurchase pp:ppurchaseResults)
                em.remove(pp);
            for (Purchase p:purchaseResults){
                em.remove(p);
            }
            em.getTransaction().commit();
            System.out.println("Επιτυχής Διαγραφή συναλλαγών!");            
            System.out.println(" ");
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        }   
                
       
    }
 
 //μειώνει το δείκτη διαθέσιμων συναλλαγών της εξωτερικής ΒΔ 
 public synchronized void decreaseAvailableTransactions(){
     if (this.availabaleTransactions>0){
        this.availabaleTransactions--;
     }
 }
 
 //αυξάνει το δείκτη διαθέσιμων συναλλαγών της εξωτερικής ΒΔ 
 public synchronized void increaseAvailableTransactions(){
        this.availabaleTransactions++;
 }
 
  //ελέγχει το δείκτη διαθέσιμων συναλλαγών της εξωτερικής ΒΔ 
 public boolean checkAvailableTransactions(){
        if (this.availabaleTransactions>0)
           return true;
        else
            return false;
 }
 

 
 
 
 }
 
